/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
include( "PP_shared.lua" )

require( "datastream" )


local Selected = nil
local SelectedPly = nil
local FriendsTable = {}		//Stream this to the server once we save them.
local PANEL = {}

function PANEL:Init()
	//Create controls here
	
	RunConsoleCommand( "SendCLFriends" )
	
	local PList = vgui.Create( "DPanelList", self )
		PList:EnableVerticalScrollbar( true )
		PList:EnableHorizontal( false )
		PList:SetSpacing( 5 )
		PList:SetPadding( 5 )
		
		self.PList = PList
		
	for k,v in pairs( player.GetAll() ) do
		if v != LocalPlayer() then self:AddPlayers( v ) end
	end
	
	local ToggleButton = vgui.Create( "DButton", self )
		ToggleButton:SetText( "Toggle friend status" )
		function ToggleButton:DoClick()
			if SelectedPly != nil then
				if table.HasValue( CLFriends, SelectedPly ) then
					for k,v in pairs( CLFriends ) do
						if v == SelectedPly then
							table.remove( CLFriends, k )
							Selected, SelectedPly = nil
							datastream.StreamToServer( "CLFriendsStream", { CLFriends } )
						break end
					end
				else
					table.insert( CLFriends, SelectedPly )
					Selected, SelectedPly = nil
					datastream.StreamToServer( "CLFriendsStream", { CLFriends } )
				end
			end
		end
		
	self.ToggleButton = ToggleButton
	
	/*local ExitButton = vgui.Create( "DButton", self )
	local SPanel = self
		ExitButton:SetText( "Save and Exit" )
		function ExitButton:DoClick()
			datastream.StreamToServer( "CLFriendsStream", { CLFriends } )
			SPanel:GetParent():GetParent():Close()
			PrintTable( CLFriends )
		end
		
	self.ExitButton = ExitButton*/
end

function PANEL:AddPlayers( ply )
	local DoPOver = false
	local PItem = vgui.Create( "DPanel", self.PList )
		PItem:SetSize( self.PList:GetWide(), 30 )
		function PItem:OnCursorEntered() DoPOver = true end
		function PItem:OnCursorExited() DoPOver = false end
		function PItem:OnMousePressed() Selected = self SelectedPly = ply end
		function PItem:Paint()
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), Color( 20, 20, 20, 255 ) )
			if DoPOver then
				draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), Color( 255, 255, 255, 40 ) )
			end
			if Selected == self then
				draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), Color( 50, 50, 200, 50 ) )
			end
			if CLFriends and table.HasValue( CLFriends, ply ) then
				draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), Color( 25, 255, 25, 50 ) )
			end
		end
		
	local PItemL = vgui.Create( "DLabel", PItem )
		PItemL:SetText( ply:Nick() )
		PItemL:SizeToContents()
		PItemL:Center()
		PItemL:AlignLeft( 30 )

	self.PList:AddItem( PItem )
end

function PANEL:PerformLayout()
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	
	self.PList:SetPos( 10, 5 )
	self.PList:SetSize( self:GetWide() - 20, self:GetTall() * 0.6 )
	
	self.ToggleButton:SetSize( 150, 35 )
	self.ToggleButton:Center()
	self.ToggleButton:MoveBelow( self.PList, 10 )
	
	//self.ExitButton:SetSize( 150, 35 )
	//self.ExitButton:Center()
	//self.ExitButton:AlignBottom( 10 )
	//Panel positioning here

end

function PANEL:Paint()
	//Do Painting here
	self.BaseClass.Paint( self )
	
	function self.PList:Paint()
		draw.RoundedBox( 2, 0, 0, self:GetWide(), self:GetTall(), Color( 225, 225, 225, 255 ) )
	end
	
	surface.SetDrawColor( 115, 115, 115, 255 )
	surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	
	surface.SetDrawColor( 95, 95, 95, 255 )
	surface.DrawRect( 8, 8, self:GetWide() - 16, self:GetTall() - 16 )

end

function PANEL:PaintOver()

	//Paint over self and child elements here.

end

function PANEL:Think()

	//Think stuff here

end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel
	Selected, SelectedPly = nil
	
end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal( key, value )

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "PPfriends", PANEL, "DPanel" )